package com.example.administrator.k3xiangmu.adapter;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.administrator.k3xiangmu.Event;
import com.example.administrator.k3xiangmu.MainActivity;
import com.example.administrator.k3xiangmu.R;
import com.example.administrator.k3xiangmu.entity.ShengchangrenwudanEntity;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2017-01-05.
 */
public class ShengchanAdapter extends BaseAdapter {
    private List<ShengchangrenwudanEntity>list;
    private Context context;
    private ProgressDialog  dialog;
    private int interid=0;

    public ShengchanAdapter(List<ShengchangrenwudanEntity> list, Context context) {
        this.list = list;
        this.context = context;
        dialog=new ProgressDialog(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView==null){
            convertView=View.inflate(context, R.layout.item_shengchanrenwudan,null);
            holder=new Holder();
            holder.tv_bianhao=(TextView)convertView.findViewById(R.id.tv_bianhao);
            holder.tv_name=(TextView)convertView.findViewById(R.id.tv_name);
            holder.tv_model=(TextView)convertView.findViewById(R.id.tv_model);
            holder.tv_time1=(TextView)convertView.findViewById(R.id.tv_sj1);
            holder.tv_time2=(TextView)convertView.findViewById(R.id.tv_sj2);
            holder.tv_no=(TextView)convertView.findViewById(R.id.tv_no);
            holder.bt=(Button)convertView.findViewById(R.id.bt_kaigong);
            convertView.setTag(holder);
        }else {
            holder=(Holder) convertView.getTag();
        }
        holder.tv_bianhao.setText(list.get(position).getFBillNo());
        holder.tv_name.setText(list.get(position).getFName());
        holder.tv_model.setText(list.get(position).getFModel());
        holder.tv_no.setText(list.get(position).getFAuxQty()+"");
        holder.tv_time1.setText("计划时间："+list.get(position).getFPlanCommitDate().substring(0,10));
        if(list.get(position).getFStartDate()==null){
            holder.tv_time2.setText("实际时间：暂未开工");
        }
        holder.bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  dialog.show();
                interid=list.get(position).getFInterID();
                list.remove(position);
                update();

            }
        });
        return convertView;
    }

    class Holder{
        private TextView tv_bianhao,tv_name,tv_model,tv_time1,tv_time2,tv_no;
        private Button bt;
    }

    private void  update(){
        dialog.show();
        UpdateTextTask2 updateTextTask = new UpdateTextTask2(context);
        updateTextTask.execute();
    }
    class UpdateTextTask2 extends AsyncTask<Void, Integer, Integer> {
        private Context context;

        UpdateTextTask2(Context context) {
            this.context = context;
        }

        /**
         * 运行在UI线程中，在调用doInBackground()之前执行
         */
        @Override
        protected void onPreExecute() {
            //dialog.show();
        }

        /**
         * 后台运行的方法，可以运行非UI线程，可以执行耗时的方法
         */
        @Override
        protected Integer doInBackground(Void... params) {

            try {

                Class.forName("net.sourceforge.jtds.jdbc.Driver");
                Connection dbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://" + MainActivity.ip + ";DatabaseName=" + MainActivity.userDB + "",
                        MainActivity.userName, MainActivity.userPwd);
                testConnection2(dbConn);


            } catch (Exception e) {
                Log.i("asd", e.toString() + "");
                return 1;

            }


            return 5;
        }

        /**
         * 运行在ui线程中，在doInBackground()执行完毕后执行
         */
        @Override
        protected void onPostExecute(Integer integer) {
            //dialog.dismiss();
            notifyDataSetChanged();
            dialog.dismiss();
            Event e=new Event();
//            int a=0;
//            int b=0;
//            int c=0;
//            for(ShengchangrenwudanEntity entity:list){
//                switch (entity.getFStatus()){
//                    case 1:a++;break;
//                    case 2:b++;break;
//                    case 3:c++;break;
//                }
//            }
//            e.setA(a);
//            e.setB(b);
//            e.setC(c);
            e.setFlag(1);

            EventBus.getDefault().post(e);

        }

        /**
         * 在publishProgress()被调用以后执行，publishProgress()用于更新进度
         */
        @Override
        protected void onProgressUpdate(Integer... values) {

        }
    }
    public void testConnection2(Connection con) throws java.sql.SQLException {

        try {

            SimpleDateFormat formatter    =   new    SimpleDateFormat    ("yyyy-MM-dd HH:mm:ss     ");
            Date    curDate    =   new Date(System.currentTimeMillis());//获取当前时间
            String    str    =    formatter.format(curDate);
            String sql = "update ICMO set FStartDate='"+str+"' ,FStatus='2' where FInterID='"+interid+"'";

            Statement stmt = con.createStatement();// 创建Statement
           stmt.executeUpdate(sql);







            stmt.close();
        } catch (SQLException e) {

            System.out.println(e.getMessage().toString() + "dddddddddddd")
            ;


        } finally {
            //Toast.makeText(MainActivity.this,ss, Toast.LENGTH_LONG).show();
            if (con != null)
                try {
                    con.close();
                } catch (SQLException e) {
                }
        }
    }

}
